﻿using System.Collections.Generic;
using Community.CsharpSqlite.WP.SqliteDataProvider.Base;

namespace Community.CsharpSqlite.WP.SqliteDataProvider
{
    public class SqliteDataReader : IDataReader
    {
        private List<DataRow> _rows;
        private int _currentOffset;

        private DataRow Current { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="System.Object"/> with the specified name.
        /// </summary>
        /// <value></value>
        public DataRow this[string name]
        {
            get { return (DataRow)Current[name]; }
            set { throw new System.NotImplementedException(); }
        }

        /// <summary>
        /// Gets field value by name
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns></returns>
        public T GetTypeOf<T>(string fieldName)
        {
            var ordinal = Current.GetOrdinal(fieldName);
            return GetTypeOf<T>(ordinal);
        }


        /// <summary>
        /// Gets field value ordinal
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="i">The i.</param>
        /// <returns></returns>
        public T GetTypeOf<T>(int i)
        {
            return Current.GetTypeOf<T>(i);
        }


        /// <summary>
        /// Gets or sets the <see cref="System.Object"/> with the specified i.
        /// </summary>
        /// <value></value>
        public DataRow this[int i]
        {
            get { return (DataRow)Current[i]; }
            set { throw new System.NotImplementedException(); }
        }


        /// <summary>
        /// Gets or sets a value indicating whether this instance is closed.
        /// </summary>
        /// <value><c>true</c> if this instance is closed; otherwise, <c>false</c>.</value>
        public bool IsClosed { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SqliteDataReader"/> class.
        /// </summary>
        /// <param name="row">The row.</param>
        public SqliteDataReader(List<DataRow> row)
        {
            _rows = row;
            _currentOffset = -1;

            if (_rows.Count > 0)
                Current = _rows[0];
        }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close()
        {
            IsClosed = true;
            _rows = null;
        }

        /// <summary>
        /// Nexts the result.
        /// </summary>
        /// <returns></returns>
        public bool NextResult()
        {
            _currentOffset++;

            if (_currentOffset >= _rows.Count)
            {
                Close();
                return false;
            }

            Current = _rows[_currentOffset];
            return true;
        }

        /// <summary>
        /// Reads this instance.
        /// </summary>
        /// <returns></returns>
        public bool Read()
        {
            return NextResult();
        }

        /// <summary>
        /// Gets or sets the records affected.
        /// </summary>
        /// <value>The records affected.</value>
        public int RecordsAffected
        {
            get { return _rows.Count; }
            set { throw new System.NotImplementedException(); }
        }


        /// <summary>
        /// Gets the schema table.
        /// </summary>
        /// <returns></returns>
        public DataTable GetSchemaTable()
        {
            throw new System.NotImplementedException();
        }


        public int Depth
        {
            get { throw new System.NotImplementedException(); }
            set { throw new System.NotImplementedException(); }
        }


    }
}
